<?php
define("APP_SECRET", "2ad42749773c441109bdc0191257a664");
define("APIKey", "85eb6835b0a1034e");
/**
* @param $params array 参数列表
* @param $key 加密密钥
* @return array sign:加密校验串,params:参数拼接串
*/
function get_sign($params, $key) {
	$_data = array();
	ksort($params);
	reset($params);
	foreach ($params as $k => $v) {
		// rawurlencode 返回的转义数字必须为大写( 如%2F )
		$_data[] = $k . '=' . rawurlencode($v);
	}
	$_sign = implode('&', $_data);
	return array(
		'sign' => strtolower(md5($_sign . $key)),
		'params' => $_sign,
	);
}

function get_bilibili($apiType, $params){
	$params["appkey"] = APIKey;
	$params["ts"] = time();
	$params["type"] = "json";
	$sign = get_sign($params, APP_SECRET);
	$url = "http://api.bilibili.com/" . $apiType . "?sign=" . $sign['sign'] . "&" . $sign['params'];
	//echo $url;
	$ch = curl_init($url);
	CURL_SETOPT($ch, CURLOPT_RETURNTRANSFER, true);
	CURL_SETOPT($ch, CURLOPT_USERAGENT, "WitHelper Weixin Client/1.0 (jesse@itjesse.cn)");
	CURL_SETOPT($ch, CURLOPT_TIMEOUT, 5);
	$result = curl_exec($ch);
	curl_close($ch);
	if($result){
		return $result;
	}else{
		return false;
	}
}

function dejson($str){
    //$str = str_replace("\\", '\\\\', $str);
    $str = str_replace("\r\n", '', $str);
    return json_decode($str);
	//return $str;
}

//$params = array(
//	'btype' => '3'
//);	
//echo $result = get_bilibili('bangumi', $params);
